package org.jfk.tbox.net;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jfk.core.utils.ByteUtils;



public class TcpClient {
	public static void main(String[] args){
		ChannelFactory factory = new NioClientSocketChannelFactory(Executors
				.newCachedThreadPool(), Executors.newCachedThreadPool());

		ClientBootstrap bootstrap = new ClientBootstrap(factory);
		TcpClientHandler handler = new TcpClientHandler();
		bootstrap.getPipeline().addLast("handler", handler);
		bootstrap.setOption("tcpNoDelay", true);
		bootstrap.setOption("keepAlive", true);

		ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(
				"119.97.185.176", 9009));
		
		
//		ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(
//				"127.0.0.1", 9009));
//		ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(
//				"202.103.63.44", 9009));
//		ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(
//		"172.16.253.1", 9009));
//		String hexString = "464C432E000069005700EA6ACD8D0000000000010401000101000012000000000000313131313131313131313131313131313131353431353032354C39393039393739313336303055303031303436303033363634313436393937340102030400FF313131313131313131313131313131313150";
//0501 464C432E00008E0057008027D6B40000000000010501000101000037000000000000313131313131313131313131313131313131353431353032354C3939303939373931333630305530303130343630303336363431343639393734010203040000000000000000000000000000008027D6B4060000000000001D000000000000000000000000000000000000000000000000000000000086
//0602 464C432E00007D00570085F549910000000000000602000107000026000000000000313131313131313131313131313131313131353431353032354C39393039393739313336303055303031303436303033363634313436393937340102030400010000000100000001000000010000400000004000404000BBD04F3A100001000008000E0000D9
//0602 3436344334333245303030303744303035373030383546353439393130303030303030303030303030363032303030313037303030303236303030303030303030303030333133313331333133313331333133313331333133313331333133313331333133313331333533343331333533303332333534433339333933303339333933373339333133333336333033303535333033303331333033343336333033303333333633363334333133343336333933393337333430313032303330343030303130303030303030313030303030303031303030303030303130303030343030303030303034303030343034303030424244303446334131303030303130303030303830303045303030304439
//0401 34363443343332453030303036393030353730304541393730314632303030303030303030303031303430313030303130313030303031323030303030303030303030303331333133313331333133313331333133313331333133313331333133313331333133313335333433313335333033323335344333393339333033393339333733393331333333363330333035353330333033313330333433363330333033333336333633343331333433363339333933373334303130323033303430304646333133313331333133313331333133313331333133313331333133313331333133313145
//0901 34363443343332453030303038443030353730304232343131324134303030303030303030303030303930313030303130313030303033363030303030303030303030303331333133313331333133313331333133313331333133313331333133313331333133313335333433313335333033323336344333393339333033393339333733393331333333363330333035353330333033313330333433363330333033333336333633343331333433363339333933373335303130323033303430303030303030303030303030303030303030303030303030303030423234313132413430363030303030303030303030303144303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303842		
		String hexString = "34363443343332453030303038443030353730304232343131324134303030303030303030303030303930313030303130313030303033363030303030303030303030303331333133313331333133313331333133313331333133313331333133313331333133313335333433313335333033323336344333393339333033393339333733393331333333363330333035353330333033313330333433363330333033333336333633343331333433363339333933373335303130323033303430303030303030303030303030303030303030303030303030303030423234313132413430363030303030303030303030303144303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303842";
		byte[] msgBuff = ByteUtils.hexStringToBytes(hexString);
		ChannelBuffer buf = ChannelBuffers.buffer(msgBuff.length);
		buf.writeBytes(msgBuff);
		channelFuture.getChannel().write(buf);
		
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.exit(0);
	}
}
